//
//  WBEmotionTool.m
//  新浪微博项目
//
//  Created by neng on 15/12/24.
//  Copyright (c) 2015年 neng. All rights reserved.
//

#import "WBEmotionTool.h"
#import "MJExtension.h"
#import "WBEmotion.h"

#define WBEmotionPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"emotions.archiver"]

@implementation WBEmotionTool

//由于是类方法，所以使用静态变量
static NSMutableArray *_recentEmotions;
static NSArray *_defaultEmotions;
static NSArray *_emojiEmotions;
static NSArray *_lxhEmotions;

+ (void)initialize
{
    _defaultEmotions = [self defaultEmotions];
    _lxhEmotions = [self lxhEmotions];
}

+ (void)addEmotion:(WBEmotion *)emotion
{
    //移除相同的表情
    [_recentEmotions removeObject:emotion];
    
    //插入到数组最前面，作为最新的表情
    [_recentEmotions insertObject:emotion atIndex:0];
    
    //归档
    [NSKeyedArchiver archiveRootObject:_recentEmotions toFile:WBEmotionPath];

}

#pragma mark - 取表情
+ (NSArray *)recentEmotions
{
    _recentEmotions = [NSKeyedUnarchiver unarchiveObjectWithFile:WBEmotionPath];
    if (!_recentEmotions) {
        _recentEmotions = [NSMutableArray array];
    }
    return _recentEmotions;
}

+ (NSArray *)defaultEmotions
{
    _defaultEmotions = [self emotionsWithPlist:@"EmotionIcons/default/info.plist"];
    return _defaultEmotions;
}

+ (NSArray *)emojiEmotions
{
    _emojiEmotions = [self emotionsWithPlist:@"EmotionIcons/emoji/info.plist"];
    return _emojiEmotions;
}

+ (NSArray *)lxhEmotions
{
    _lxhEmotions = [self emotionsWithPlist:@"EmotionIcons/lxh/info.plist"];
    return _lxhEmotions;
}

//取出plist的表情
+ (NSArray *)emotionsWithPlist:(NSString *)plist
{
    //一定要用想转的这个类名调这个方法
    NSArray *modelArray = [WBEmotion objectArrayWithFilename:plist];
    
    return modelArray;
}

//根据中文取表情
+ (WBEmotion *)emotionWith:(NSString *)chs
{
    for (WBEmotion *emotion in _defaultEmotions) {
        if ([emotion.chs isEqualToString:chs]) {
            return emotion;
        }
    }
    for (WBEmotion *emotion in _lxhEmotions) {
        if ([emotion.chs isEqualToString:chs]) {
            return emotion;
        }
    }    
    return nil;
}

@end
